<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-06-17
 * Time: 15:16:54
 * Info:
 */

namespace app\admin\controller\system;

use app\admin\annotation\ControllerAnnotation;
use app\admin\annotation\NodeAnotation;
use app\admin\controller\CommonController;
use support\Request;
use think\facade\Cache;
use app\common\model\AuthMenu as AuthMenuModel;
use app\common\model\AuthRule as AuthRuleModel;

/**
 * @ControllerAnnotation(title="后台菜单管理")
 * Class Node
 * @package app\admin\controller\system
 */
class AuthMenuController extends CommonController
{

    /**
     * @NodeAnotation(title="菜单列表")
     */
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $cacheData = Cache::get('cacheSystemMenu');
            if ( ! empty($cacheData)) {
                $list = $cacheData;
            } else {
                $list = AuthMenuModel::order('sort asc,id asc')->select()->toArray();
                Cache::set('cacheSystemMenu', $list);
            }
            $count = AuthMenuModel::count();

            return $this->success('ok', $list, $count);
        }

        return view('auth_menu/index');
    }

    /**
     * @NodeAnotation(title="添加菜单")
     */
    public function add(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->post();
            $this->validate->rule([
                'title|菜单名称' => 'require',
                'node|菜单地址'  => 'require',
            ]);
            $this->validate->message([
                'title.require' => '菜单菜单名称不能为空',
                'node.require'  => '菜单菜单地址不能为空',
            ]);
            if ( ! $this->validate->check($param)) {
                return error_json($this->validate->getError());
            }
            $res = AuthMenuModel::manageAdd($param);
            if ($res['code'] == 200) {
                Cache::clear();

                return $this->success($res['msg']);
            } else {
                return error_json($res['msg']);
            }
        }
        $id      = $request->get('id');
        $pidList = AuthMenuModel::getPidMenuList();

        return view('auth_menu/add', ['pid_list' => $pidList, 'id' => $id]);
    }

    /**
     * @NodeAnotation(title="编辑菜单")
     */
    public function edit(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->post();
            $this->validate->rule([
                'title|菜单菜单名称' => 'require',
                'node|菜单地址'      => 'require',
            ]);
            $this->validate->message([
                //'id.require'    => '规则id不能为空',
                'title.require' => '菜单菜单名称不能为空',
                'node.require'  => '规则地址不能为空',
            ]);
            if ( ! $this->validate->check($param)) {
                return error_json($this->validate->getError());
            }
            $param['node'] = strtolower($param['node']);
            $res           = AuthMenuModel::manageEdit($param);
            if ($res['code'] == 200) {
                Cache::clear();

                return $this->success($res['msg']);
            } else {
                return error_json($res['msg']);
            }
        }

        $id   = $request->get('id');
        $data = AuthMenuModel::findData($id);
        if ($data['code'] == 0) {
            return error_json('获取数据失败');
        }
        $pidList = AuthMenuModel::getPidMenuList();

        return view('auth_menu/edit', ['data' => $data['data'], 'pid_list' => $pidList]);
    }

    /**
     * @NodeAnotation(title="删除菜单")
     */
    public function delete(Request $request)
    {
        if ($request->isAjax()) {
            $id = $request->get('id');
            if (empty($id)) {
                return error_json('规则id不能为空');
            }
            $res = AuthMenuModel::manageDel($id);
            if ($res['code'] == 200) {
                Cache::clear();

                return $this->success($res['msg']);
            } else {
                return error_json($res['msg']);
            }
        }
    }

    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify(Request $request)
    {
        $param = $request->all();
        $this->validate->rule([
            'id|ID'      => 'require',
            'field|字段' => 'require',
            'val|值'     => 'require',
        ]);
        if ( ! $this->validate->check($param)) {
            return error_json($this->validate->getError());
        }
        $res = AuthMenuModel::manageModify($param);
        if ($res['code'] == 200) {
            Cache::clear();

            return $this->success($res['msg']);
        } else {
            return error_json($res['msg']);
        }
    }

}
